﻿using FileToVideo.Class;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileToVideo.AddFunc
{
    public static class KeyColorAdd
    {
        /// <summary>
        /// 转为字典
        /// </summary>
        /// <param name="keyColor"></param>
        /// <returns></returns>
        public static Dictionary<string,string> ToDict(this KeyColor keyColor)
        {
            var dict = new Dictionary<string,string>();
            dict.Add(keyColor.Key.ToString(), keyColor.Color);
            return dict;
        }

        /// <summary>
        /// 转为字典
        /// </summary>
        /// <param name="keyColorList"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ToDict(this List<KeyColor> keyColors)
        {
            var dict = new Dictionary<string, string>();
            foreach (var keyColor in keyColors)
            {
                dict.Add(keyColor.Key.ToString(),keyColor.Color);
            }
            return dict;
        }
        
        /// <summary>
        /// 从字典中读取
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static KeyColor FromDict(this KeyColor keyColor, Dictionary<string,string> dict)
        {
            if (dict.Count ==0 )
            {
                return null;
            }
            else
            {
                foreach (var item in dict)
                {
                    keyColor.Key = char.Parse(item.Key);
                    keyColor.Color = item.Value;
                    break;
                }
                return keyColor;
            }
        } 
    }
}
